//
//  JGAuthenticationCell.m
//  JGRider
//
//  Created by 单启志 on 2018/2/9.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGAuthenticationCell.h"
#import "LMJElementsFlowLayout.h"
#import "JGAuthenticationCollectionCell.h"
#define collectionW (JGScreenW -  AdaptedWidth(16)*2-AdaptedWidth(10)*2) / 3
@interface JGAuthenticationCell()<UICollectionViewDelegate,UICollectionViewDataSource,LMJElementsFlowLayoutDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@property (weak, nonatomic) IBOutlet UILabel *conentLab;

@property (weak, nonatomic) IBOutlet UICollectionView *colllectionView;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewConstH;

@property(nonatomic,strong)NSIndexPath *collectionSelectIndexPath;
@end

@implementation JGAuthenticationCell
static NSString *collectionCellId=@"authenticationCollectionCell";
- (void)awakeFromNib {
    [super awakeFromNib];
    [self configSubViews];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

-(void)configSubViews{
    
    self.collectionViewConstH.constant=collectionW;
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    LMJElementsFlowLayout *flowLaout=[LMJElementsFlowLayout flowLayoutWithDelegate:self];
    self.colllectionView.collectionViewLayout=flowLaout;
    self.colllectionView.delegate=self;
    self.colllectionView.dataSource=self;
    self.colllectionView.bounces=NO;
    [self.colllectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JGAuthenticationCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:collectionCellId];
}

- (CGSize)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
     return CGSizeMake(collectionW, collectionW);
}
- (UIEdgeInsets)waterflowLayout:(LMJElementsFlowLayout *)waterflowLayout edgeInsetsInCollectionView:(UICollectionView *)collectionView{
    return UIEdgeInsetsZero;
}

#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.images.count +1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    JGAuthenticationCollectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:collectionCellId forIndexPath:indexPath];
     BOOL isLastCell= indexPath.row==self.model.images.count;
    if (isLastCell) {
        cell.image=[UIImage imageNamed:@""];
    }else{
         cell.image=self.model.images[indexPath.row];
    }
    
    cell.removeBtn.hidden=isLastCell;
    [cell.removeBtn addTarget:self action:@selector(collectionCellRemoveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.collectionSelectIndexPath=indexPath;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [self setSelected:YES animated:YES];
    if (indexPath.row==self.model.images.count) {
        if ([self.delegate respondsToSelector:@selector(collectionCellDidSelectItemAtIndexPath:selectCell:)]) {
            [self.delegate collectionCellDidSelectItemAtIndexPath:indexPath selectCell:self];
        }
    }else{//预览图片
        if ([self.delegate respondsToSelector:@selector(collectionCellPreviewItemAtIndexPath:selectCell:)]) {
            [self.delegate collectionCellPreviewItemAtIndexPath:indexPath selectCell:self];
        }
        
    }
    
    
}

//删除图片btn
-(void)collectionCellRemoveBtnAction{
    
    if ([self.delegate respondsToSelector:@selector(collectionCellRemoveImageItemAtIndexPath:selectCell:)]) {
        NSLog(@"aaaaaaaa");
        [self.delegate collectionCellRemoveImageItemAtIndexPath:self.collectionSelectIndexPath selectCell:self];
    }
}

#pragma mark - setters
-(void)setModel:(JGAuthenticationModel *)model{
    _model=model;
    self.titleLab.text=model.title;
    self.conentLab.text=model.content;
   NSInteger rows=model.images.count/3 +1;
   CGFloat collWidth=collectionW*rows+AdaptedWidth(10)*(rows-1);
    
    self.collectionViewConstH.constant=collWidth;
    [self layoutIfNeeded];
    [self.colllectionView reloadData];
}


@end
